home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / SASC_6.0_Disk_7.adf / Source_And_Examples / extras / Memlib / mwalcfre.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-30  |  4.1 KB  |  157 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2. * |_o_o|\\ Copyright (c) 1989 The Software Distillery.                    *
  3. * |. o.| ||          All Rights Reserved                                  *
  4. * | .  | ||          Written by Doug Walker                               *
  5. * | o  | ||          The Software Distillery                              *
  6. * |  . |//           235 Trillingham Lane                                 *
  7. * ======             Cary, NC 27513                                       *
  8. *                    BBS:(919)-471-6436                                   *
  9. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  10.  
  11. #include "mempriv.h"
  12.  
  13. extern struct MWGlobal mwg;
  14.  
  15. void *MWAllocMem(long size, long flags, long ismalloc, char *file, long line)
  16. {
  17.    struct MWAlc *hd;
  18.    char *tmpchar;
  19.    int memtype;
  20.  
  21.    /* Force warning for malloc'd memory not freed if requested */
  22.    if(mwg.flags & MWF_MALLOCWRN) ismalloc = 0;
  23.  
  24.    if(!(mwg.flags & MWF_ACTIVE)) return(NULL);
  25.    if(mwg.flags & MWF_CHECK) MWCheck();
  26.  
  27.    if(size <= 0)
  28.    {
  29.       MWPrintf("\7Bad AllocMem length %ld from line %ld file \"%s\"\n",
  30.          size, line, file);
  31.       return(NULL);
  32.    }
  33.  
  34.    memtype = (flags & MEMF_CHIP ? MWT_CHIP : MWT_FAST);
  35.    if(mwg.sum[memtype] + size > mwg.lim[memtype])
  36.    {
  37.       /* Over the limit, fail it */
  38.       MWPrintf("MemWatch: ");
  39.       if(memtype == MWT_CHIP)
  40.          MWPrintf("CHIP ");
  41.       else
  42.          MWPrintf("FAST ");
  43.       MWPrintf("memory allocation exceeds MWLimit amount\n");
  44.       return(NULL);
  45.    }
  46.  
  47.    while(!(tmpchar = (char *)AllocMem(sizeof(struct MWAlc)+size, flags)))
  48.    {
  49.       if(mwg.freed) MWPurge();
  50.       else return(NULL);
  51.    }
  52.    
  53.    hd = (struct MWAlc *)tmpchar;
  54.    hd->size = size;
  55.    hd->flags = flags;
  56.    hd->myflags = (ismalloc ? MWF_ISMALLOC : 0L);
  57.    hd->file = file;
  58.    hd->line = line;
  59.    memcpy(hd->header, MWHEADSTR, MW_HEADLEN);
  60.    memcpy(hd->memory+size, MWTRAILSTR, MW_TRAILLEN);
  61.  
  62.    if(!(flags & MEMF_CLEAR) && !(mwg.flags & MWF_NOATRASH))
  63.       memset(hd->memory, MWATRASH, size);   /* Trash the memory */
  64.  
  65.    hd->next = mwg.first;
  66.    mwg.first = hd;
  67.  
  68.    if((mwg.sum[memtype] += size) > mwg.max[memtype]) 
  69.       mwg.max[memtype] = mwg.sum[memtype];
  70.    ++(mwg.num[memtype]);
  71.  
  72.    return((char *)hd->memory);
  73. }
  74.  
  75. void MWFreeMem(void *mem, long size, long internal, 
  76.                char *file, long line)
  77. {
  78.    struct MWAlc *mwa, *prev;
  79.    int memtype;
  80.    int error = 0;
  81.  
  82.    if(!(mwg.flags & MWF_ACTIVE)) return;
  83.    if(mwg.flags & MWF_CHECK) MWCheck();
  84.  
  85.    for(prev = NULL, mwa = mwg.first; 
  86.        mwa && mwa->memory != mem; 
  87.        prev = mwa, mwa = mwa->next);
  88.  
  89.    if(!mwa)
  90.    {
  91.       for(mwa=mwg.freed; mwa && mwa->memory != mem; mwa=mwa->next);
  92.       if(mwa)
  93.       {
  94.          MWPrintf("\7Memory freed twice\n");
  95.          MWPrintAlc(mwa);
  96.       }
  97.       MWPrintf("\7Invalid FreeMem call, addr 0x%08lx length %ld\n"\
  98.                   "                       line %ld file \"%s\"\n",
  99.             mem, size, line, file);
  100.       error = 1;
  101.    }
  102.    else if(!internal && mwa->size != size)
  103.    {
  104.       MWPrintf("\7Incorrect FreeMem length %ld line %ld file \"%s\"\n", 
  105.          size, line, file);
  106.       MWPrintAlc(mwa);
  107.       error = 2;
  108.    }
  109.    else if(MWCheckA(mwa))
  110.      error = 2;
  111.  
  112.    if(error)
  113.    {
  114.       MWHold();
  115.       if(error == 1) return;
  116.    }
  117.  
  118.    memtype = (mwa->flags & MEMF_CHIP ? MWT_CHIP : MWT_FAST);
  119.    mwg.sum[memtype] -= mwa->size;
  120.    --mwg.num[memtype];
  121.  
  122.    if(prev) prev->next = mwa->next;
  123.    else     mwg.first = mwa->next;
  124.  
  125.    if(!(mwg.flags & MWF_NOFTRASH))
  126.       memset(mwa->memory, MWFTRASH, mwa->size);  /* Trash it */
  127.  
  128.    if(mwg.flags & MWF_NOFKEEP)
  129.      FreeMem((char *)mwa, mwa->size + sizeof(struct MWAlc));
  130.    else
  131.    {
  132.       mwa->next = mwg.freed;
  133.       mwg.freed = mwa;
  134.    }
  135. }
  136.  
  137. void *MWrealloc(void *mem, long size, char *file, long line)
  138. {
  139.    void *new;
  140.    struct MWAlc *mwa;
  141.  
  142.    if(!(new = MWAllocMem(size, 0, 1, file, line)))
  143.       return(NULL);
  144.  
  145.    for(mwa = mwg.first; 
  146.        mwa && mwa->memory != mem; 
  147.        mwa = mwa->next);
  148.  
  149.    if(mwa && mwa->size < size) size = mwa->size;
  150.  
  151.    memcpy(new, mem, size);
  152.  
  153.    MWFreeMem(mem, 0, 1, file, line);
  154.  
  155.    return(new);
  156. }
  157.